<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template #compute_code="{row}">
      <div class="flex">
        <input type="hidden" :name="row.name" :value="row.value">
        <p>{{row.value}}</p>
        <el-button @click="set_compute_code(row)">设置计算值</el-button>
      </div>
    </template>
  </forms>
</template>

<script>
import {decode_config, s94_event} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {error_back, field_type, iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import {S94Model} from "@/js/S94Model.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let model_name = this.$get('model_name');
    let model = S94Model.find(model_name);
    if (!model) error_back(`模型不存在：model_name=${model_name}`);

    let type_map = field_type(field_type.CAN_PARAM, 'label');

    let forms_config = {
      param_type: {label:'参数类型',name:'param_type',type:'hidden',value: 'edit'},
      model_name: {label:'模型',name:'model_name',type:'hidden',value: model_name},
      field: {label:'数据库字段',name:'field',type:'hidden',tips:'参数写入的数据库字段，只能是当前表存在的字段',value:'public_param_json'},
      name: {label:'参数名',name:'name',type:'text_one',tips:'字段名，[0-9a-z_]'},
      label: {label:'别名',name:'label',type:'text_one',tips:''},
      tips: {label:'提示',name:'tips',type:'text_one',tips:'参数输入的提示'},

      type: {label:'组件类型',name:'type',type:'radio',config:type_map,tips:'选择参数的组件类型',value:''},
      config: {label:'组件配置',name:'config',type:'text_one',computed:true,tips:''},

      value: {label:'默认值',name:'value',type:'text_one',computed:true,tips:'当参数值为空（null,empty,空字符）时，参数值会使用该默认值'},

      can_edit:{label:'可编辑', name:'can_edit', type:'hidden', value:1},
      validate: {label:'验证规则',name:'validate',type:'validate_rule', value:'[]'},

      handle_type: {label:'处理类型',name:'handle_type',type:'hidden',tips:'',value:'json'},

      is_public:{label:'是否公开', name:'is_public', type:'hidden',value:1},

      sort: {label:'排序',name:'sort',type:'hidden',tips:'排序索引，大>小', value:0},
    }
    if (id){
      forms_config.model_name.is_info = true;
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      model_name: model_name,
      btns: btns,
      config: forms_config,
      is_default: 0,
      name_has_point: false,
    }
  },
  methods: {
    param(data){
      data = data || {};
      data.model_name = this.model_name;
      return s94Mjs.param_encode(data);
    },
    onsubmit(formData){
      let url = this.id ? `/admin/model_v2_form_param/edit?id=`+this.id : '/admin/model_v2_form_param/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('model_v2_form_param/index_public?'+this.param(), '$refs.list_url.list');
          iframe.hide('model_v2_form_param/edit_public');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
        s94_event(`model_v2_form_param/${this.id?'edit':'add'}`, res);
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
    set_compute_code(row, model_name){
      let app = this;
      model_name = model_name || "";
      iframe({
        href: '/index/compute_code?model_name='+model_name,
        btns: {
          '确定_primary'(win_app){
            row.value = win_app.compute_code;
            win_app.__iframe.show = false;
          }
        }
      });
    },
  },
  watch: {
    'config.field.value'(v){
      if (!this.config.name.value) this.config.name.value = v.split(',').shift();
    },
    'config.can_edit.value'(v){
      // this.config.validate.remove = (v==0); //无论是否可编辑，可以设定验证规则，例如添加时，需要当前用户ID，需要验证值大于0
    },
    'config.type.value'(v){
      this.config.value.type = v=="text_more" ? "text_more" : "text_one";
      s94Mjs.each(field_type.component_config(v), (v,k)=>{
        this.config.config[k] = v;
      })
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/model_v2_form_param/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('model_v2_form_param/edit_public');
        }else {
          this.$router.push('index');
        }
      })
    }else {
      this.config.type.value = 'text_one';
    }

    //其他初始化操作
  }
}
</script>
